package com.example.rtmppush;

import android.hardware.display.DisplayManager;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.projection.MediaProjection;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class H264Encoder extends Thread{

    private String TAG ="H264Encoder";

    int width = 720;
    int height = 1080;
    MediaCodec mediaCodec;
    MediaProjection mediaProjection;

    boolean isLive = true;
    String path ;
    public H264Encoder(MediaProjection mediaProjection, String path) {

        this.path = path;
        this.mediaProjection = mediaProjection;
        initEncoder();
    }

    private void initEncoder() {

        try {
            mediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);

            MediaFormat videoFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, width, height);
            videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE,15);
            videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,30);
            videoFormat.setInteger(MediaFormat.KEY_BIT_RATE,width * height);
            videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
            mediaCodec.configure(videoFormat,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE);
            Surface inputSurface = mediaCodec.createInputSurface();
            mediaProjection.createVirtualDisplay("daidai",width,height,2, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,inputSurface,null,null);
            mediaCodec.start();
             } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG,"不支持的编码");
        }
    }

    @Override
    public void run() {
        super.run();

        MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

        while (isLive) {
            int outIndex = mediaCodec.dequeueOutputBuffer(bufferInfo,1000);
            if (outIndex >=0) {
                ByteBuffer outputBuffer = mediaCodec.getOutputBuffer(outIndex);
                byte[] buff = new byte[outputBuffer.remaining()];
                outputBuffer.get(buff);
                // 保存文件
                writeFile(buff);
                mediaCodec.releaseOutputBuffer(outIndex,false);
            }
        }
    }

    void writeFile(byte[] buff) {
//      String path =  Environment.getExternalStorageDirectory().getAbsolutePath() + "/ScreenPush";
        File file = new File(path);

        if (!file.exists()) {
            boolean mkdir = file.mkdirs();
            Log.e(TAG,"path 创建成分股"+mkdir);
        }else  {
            Log.e(TAG,"path 存在"+path);
        }

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path+"//screen.h264",true);
            try {
                fileOutputStream.write(buff);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
